package com.divine.wn.car.admin.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.divine.wn.car.admin.form.MaintainVehicleForm;
import com.divine.wn.car.admin.vo.AreaVO;
import com.divine.wn.car.admin.vo.RoleVO;
import com.divine.wn.car.admin.vo.UserVO;
import com.divine.wn.car.admin.vo.VehicleVO;

	public class MaintainVehicleAction extends BaseAction {

	// 获取车辆类型
	public ActionForward getVehicleTypeList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		MaintainVehicleForm maintainVehicleForm = (MaintainVehicleForm) form;

		maintainVehicleForm.setVehicleList(maintainVehicleService
				.getVehicleTypeList());

		return mapping.findForward("vehicleList");
	}
	

		// 获取车辆类型
		public ActionForward getTxList(ActionMapping mapping,
				ActionForm form, HttpServletRequest request,
				HttpServletResponse response) throws Exception {
			MaintainVehicleForm maintainVehicleForm = (MaintainVehicleForm) form;

			maintainVehicleForm.setTxMap(maintainVehicleService.getTxList());

			return mapping.findForward("txList");
		}
	
		
		public ActionForward updateTx(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response)
				throws Exception {
			MaintainVehicleForm maintainVehicleForm = (MaintainVehicleForm) form;


			maintainVehicleService.updateTx(maintainVehicleForm.getTxMap());
			return mapping.findForward("txList");
		}
		
	public ActionForward updateVehicle(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MaintainVehicleForm maintainVehicleForm = (MaintainVehicleForm) form;

		List<VehicleVO> vehicleList = maintainVehicleForm.getVehicleList();

		maintainVehicleService.updateVehicle(vehicleList);
		maintainVehicleForm.setVehicleList(maintainVehicleService
				.getVehicleTypeList());
		return mapping.findForward("vehicleList");
	}

	// 跳转到套餐维护页面

	public ActionForward toTC(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		this.getArea(form, request);
		request.setAttribute("tcList", maintainVehicleService.getTC());

		return mapping.findForward("tc");
	}

	private void getArea(ActionForm form, HttpServletRequest request)
			throws Exception {
		List<HashMap<String, String>> areas = baseDataService.getAreaData();
		request.setAttribute("areas", areas);

	}

	// 跳转到地区维护页面

	public ActionForward getAreaList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MaintainVehicleForm maintainVehicleForm = (MaintainVehicleForm) form;
		maintainVehicleForm.setAreaList(maintainVehicleService
				.getAreaDataList());
		return mapping.findForward("areaList");

	}

	// 删除地区
	public ActionForward deleteArea(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MaintainVehicleForm maintainVehicleForm = (MaintainVehicleForm) form;
		maintainVehicleService.deleteArea(request.getParameter("areaID"));
		maintainVehicleForm.setAreaList(maintainVehicleService
				.getAreaDataList());

		return mapping.findForward("areaList");

	}

	// 新增或者更新地区
	public ActionForward updateArea(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MaintainVehicleForm maintainVehicleForm = (MaintainVehicleForm) form;
		AreaVO areaVO = new AreaVO();

		areaVO.setAreaID(request.getParameter("areaID"));

		areaVO.setArea(request.getParameter("area"));
		areaVO.setKhscf(request.getParameter("khscf"));

		if (areaVO.getAreaID().equals("")) {
			maintainVehicleService.insertArea(areaVO);
		} else {
			maintainVehicleService.updateArea(areaVO);

		}

		maintainVehicleForm.setAreaList(maintainVehicleService
				.getAreaDataList());

		return mapping.findForward("areaList");

	}
	
	// 跳转到角色维护页面

	public ActionForward getRoleList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MaintainVehicleForm maintainVehicleForm = (MaintainVehicleForm) form;
		maintainVehicleForm.setRoleList(maintainVehicleService.getRoleDataList());
		return mapping.findForward("roleList");

	}

	// 删除角色
	public ActionForward deleteRole(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MaintainVehicleForm maintainVehicleForm = (MaintainVehicleForm) form;
		maintainVehicleService.deleteRole(request.getParameter("roleID"));
		maintainVehicleForm.setRoleList(maintainVehicleService.getRoleDataList());

		return mapping.findForward("roleList");

	}

	// 新增或者更新角色
	public ActionForward updateRole(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MaintainVehicleForm maintainVehicleForm = (MaintainVehicleForm) form;
		RoleVO RoleVO = new RoleVO();

		RoleVO.setRoleID(request.getParameter("roleID"));

		RoleVO.setRoleName(request.getParameter("roleName"));

		if (RoleVO.getRoleID().equals("")) {
			maintainVehicleService.insertRole(RoleVO);
		} else {
			maintainVehicleService.updateRole(RoleVO);

		}

		maintainVehicleForm.setRoleList(maintainVehicleService.getRoleDataList());

		return mapping.findForward("roleList");

	}
	

	public ActionForward updateTC(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		HashMap<String, Object> tcMap = new HashMap<String, Object>();
		tcMap.put("tcid", request.getParameter("tcid"));
		tcMap.put("tcname", request.getParameter("tcname"));

		tcMap.put("sxf", request.getParameter("sxf"));
		tcMap.put("sxfl", request.getParameter("sxfl"));

		tcMap.put("dygzfd", request.getParameter("dygzfd"));
		tcMap.put("dygzfx", request.getParameter("dygzfx"));
		tcMap.put("dygzfl", request.getParameter("dygzfl"));

		tcMap.put("dbf", request.getParameter("dbf"));
		tcMap.put("dbfl", request.getParameter("dbfl"));

		tcMap.put("cdfwf1", request.getParameter("cdfwf1"));
		tcMap.put("cdfwf2", request.getParameter("cdfwf2"));
		tcMap.put("cdfwf3", request.getParameter("cdfwf3"));
		tcMap.put("cdfwfl", "1");
		
		tcMap.put("gpsf1", request.getParameter("gpsf1"));
		tcMap.put("gpsf2", request.getParameter("gpsf2"));
		tcMap.put("gpsf3", request.getParameter("gpsf3"));
		tcMap.put("gpsfl", "1");

		tcMap.put("jgfd", request.getParameter("jgfd"));
		tcMap.put("jgfl", request.getParameter("jgfl"));
		tcMap.put("jgfx", request.getParameter("jgfx"));

		tcMap.put("bzj", request.getParameter("bzj"));
		tcMap.put("bzjl", request.getParameter("bzjl"));

		tcMap.put("ys", request.getParameter("ys"));
		tcMap.put("ysl", request.getParameter("ysl"));

		if (!request.getParameter("tcid").equals("")) {
			maintainVehicleService.updateTC(tcMap);
		} else {
			maintainVehicleService.insertTC(tcMap);
		}
		this.getArea(form, request);
		request.setAttribute("tcList", maintainVehicleService.getTC());
		return mapping.findForward("tc");
	}

	public ActionForward deleteTC(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		HashMap<String, Object> tcMap = new HashMap<String, Object>();
		tcMap.put("tcid", request.getParameter("tcid"));

		maintainVehicleService.deleteTC(tcMap);

		this.getArea(form, request);
		request.setAttribute("tcList", maintainVehicleService.getTC());
		return mapping.findForward("tc");
	}

	public ActionForward getDataList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		MaintainVehicleForm maintainVehicleForm = (MaintainVehicleForm) form;

		HashMap<String, Object> map = new HashMap<String, Object>();

		if (!StringUtils.isBlank(maintainVehicleForm.getUserVO().getUser_sn_search() )) {
			map.put("usersn", maintainVehicleForm.getUserVO()
					.getUser_sn_search());
		}
		if (!StringUtils.isBlank(maintainVehicleForm.getUserVO().getUser_name_search())) {
			map.put("username", maintainVehicleForm.getUserVO()
					.getUser_name_search());
		}
		if (maintainVehicleForm.getUserVO().getBuserid_search() != null
				&& !maintainVehicleForm.getUserVO().getBuserid_search().trim()
						.equals("-1")) {
			map.put("buserid", maintainVehicleForm.getUserVO()
					.getBuserid_search());
		}

		if (maintainVehicleForm.getUserVO().getBuserid_search() != null
				&& !maintainVehicleForm.getUserVO().getYear_search().trim()
						.equals("-1")) {
			map.put("year", maintainVehicleForm.getUserVO().getYear_search());
		}

		if (maintainVehicleForm.getUserVO().getBuserid_search() != null
				&& !maintainVehicleForm.getUserVO().getMonth_search().trim()
						.equals("-1")) {
			map.put("month", maintainVehicleForm.getUserVO().getMonth_search());
		}

		int dataCount = maintainVehicleService.getDataCount(map);

		request.setAttribute("total", dataCount);
		if (request.getParameter("pager.offset") == null) {
			map.put("skip", 0);
		} else {
			if (new Integer(request.getParameter("pager.offset")) != 0) {
				map.put("skip",
						new Integer(request.getParameter("pager.offset")));
			} else {
				map.put("skip", 0);
			}

		}
		request.setAttribute("url", "getDataList");
		request.setAttribute("maxCount", maintainVehicleForm.getMaxCount());
		map.put("limit", maintainVehicleForm.getMaxCount());

		List<HashMap<String, Object>> dataList = null;
		if (dataCount == 0) {
			dataList = new ArrayList<HashMap<String, Object>>();
		} else {
			dataList = maintainVehicleService.getDataList(map);
		}
		request.setAttribute("dateList", dataList);
		
		request.setAttribute("ywyList", maintainVehicleService.getYWY());

		return mapping.findForward("dataList");
	}

	// 业务员删除用户
	public ActionForward deleteUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MaintainVehicleForm maintainVehicleForm = (MaintainVehicleForm) form;

		HashMap<String, Object> map = new HashMap<String, Object>();

		maintainVehicleService.deleteDataUser(new Integer(maintainVehicleForm
				.getTableID()));

		return this
				.getDataList(mapping, maintainVehicleForm, request, response);
	}

	public ActionForward getUserInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		MaintainVehicleForm maintainVehicleForm = (MaintainVehicleForm) form;

		UserVO userVO = maintainVehicleService.getFrontUserInfo(new Integer(
				maintainVehicleForm.getTableID()));

	
		maintainVehicleForm.setUserVO(userVO);

//		request.setAttribute(
//				"payList",
//				maintainVehicleService.getPay(maintainVehicleForm.getUserVO()
//						.getId() + ""));

		// 查询日志列表
		request.setAttribute("logList", logService.getLogList(new Integer(
				maintainVehicleForm.getTableID())));

		return mapping.findForward("info");
	}

	public ActionForward viewYJ(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		request.setAttribute("ywyList", maintainVehicleService.getYWY());
		request.setAttribute("chartMapList", new ArrayList());
		return mapping.findForward("yj");
	}

	public ActionForward getChartData(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("year", request.getParameter("userVO.year_search"));
		if (request.getParameter("userVO.buserid_search").equals("-1")) {
			map.put("buserid", null);
		} else {
			map.put("buserid", request.getParameter("userVO.buserid_search"));
		}

		request.setAttribute("ywyList", maintainVehicleService.getYWY());
		List<HashMap<String, Object>> chartMapList = maintainVehicleService
				.getChartData(map);
		request.setAttribute("chartMapList", chartMapList);
		return mapping.findForward("yj");
	}

}
